﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using ZoneFiveSoftware.Common.Visuals.Fitness;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;
using System.Xml;
using System.Globalization;
using System.ComponentModel;

namespace HackersDiet
{
    public class Plugin : IPlugin
    {
        #region Members

        private static Container container = new Container();
        private static IApplication application = null;
        private HDControl hdControl = new HDControl();
        private Chart resetChart = new Chart();
        private Chart defaultChart = new Chart();
        private static Version version = Assembly.GetExecutingAssembly().GetName().Version;

        #region Constructor

        public Plugin()
        {
            Plugin.Container.Add(this.hdControl, "HDControl");
            Plugin.Container.Add(this.resetChart, "ResetChart");
            Plugin.Container.Add(this.defaultChart, "DefaultChart");
        }

        #endregion

        public static IApplication GetApplication()
        {
            return application;
        }

        public static IContainer Container
        {
            get
            {
                return Plugin.container;
            }
        }

        #endregion

        #region IPlugin Members

        public IApplication Application
        {
            set { application = value; }
        }
                
        public Guid Id
        {
            get { return new Guid("1be7eb73-f5cf-494e-8d4a-f48d54c7e53a"); }
        }

        public string Name
        {
            get { return "HDPlugin"; }
        }

        public void ReadOptions(System.Xml.XmlDocument xmlDoc, System.Xml.XmlNamespaceManager nsmgr, System.Xml.XmlElement pluginNode)
        {
            try
            {
                if (!string.IsNullOrEmpty(pluginNode.InnerXml))
                {
                    StringReader sr = new StringReader(pluginNode.InnerXml);
                    this.hdControl.Chart.Serializer.IsUnknownAttributeIgnored = true;
                    this.hdControl.Chart.Serializer.Load(sr);
                    this.resetChart.Serializer.IsUnknownAttributeIgnored = true;
                    this.resetChart.Serializer.Load(sr);
                }
            }
            catch(Exception)
            {
            }
        }

        public string Version
        {
            get { return string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build); }
        }

        public void WriteOptions(System.Xml.XmlDocument xmlDoc, System.Xml.XmlElement pluginNode)
        {
            if (this.hdControl.Chart == null)
                return;

            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
            this.hdControl.Chart.Serializer.Content = System.Windows.Forms.DataVisualization.Charting.SerializationContents.All;
            this.hdControl.Chart.Serializer.NonSerializableContent = "*.Points";

            this.hdControl.Chart.Serializer.Save(sw);

            string text = sw.ToString();

            pluginNode.Prefix = string.Empty;
            pluginNode.InnerXml = text.Substring(text.IndexOf('<', 1));

            return;
        }

        #endregion
    }
}
